<?php
namespace app\api\model;

use think\Model;
use think\Request;
use think\Db;
class Service extends Model
{
    protected $tableName;
    //构造方法，实例化表名
    public function __construct($tableName = ''){
        $this->tableName = $tableName;
    }
    /* 查询表连接另外2张表
     * $type1:数据表1设置别名1
     * $type2:数据表2设置别名1
     * $where:条件（带别名条件）
     * $condition1:连接条件1
     * $condition2:连接条件2
     * $limit:查询条数
     * $page:页数
     * $fields:需要查询的数据字段
     * */
    public function service_list($type1,$type2,$where,$condition1,$condition2,$fields,$limit,$page,$order){
        $data=Db::name($this->tableName)->alias('w')->field($fields)
            ->join($type1,$condition1,'left')
            ->join($type2,$condition2,'left')
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->page($page)->select();
        return $data;
    }
    /* 查询表连接另外2张表
     * $type1:数据表1设置别名1
     * $type2:数据表2设置别名1
     * $where:条件（带别名条件）
     * $condition1:连接条件1
     * $condition2:连接条件2
     * $limit:查询条数
     * $page:页数
     * $fields:需要查询的数据字段
     * */
    public function service_detail($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields){
        $data=Db::name($this->tableName)->alias('w')->field($fields)
            ->join($type1,$condition1,'left')
            ->join($type2,$condition2,'left')
            ->join($type3,$condition3,'left')
            ->where($where)
            ->find();
        return $data;
    }
    /* 查询表中符合条件的所有数据
     * $alias:数据表别名
     * $where:条件（带别名条件）
     * */
    public function count($type1,$type2,$where,$condition1,$condition2)
    {
        $data = Db::name($this->tableName)->alias('w')
            ->join($type1,$condition1,'left')
            ->join($type2,$condition2,'left')
            ->where($where)
            ->count();
        return $data;
    }
    /* 查询服务表中的交易量
     * $alias:数据表别名
     * $where:条件（带别名条件）
     * */
    public function num($where)
    {
        $data = Db::name($this->tableName)
            ->where($where)
            ->count();
        return $data;
    }
    /* 查询店铺标签
     * 
     * */
    public function label($where,$fields)
    {
        $data = Db::name($this->tableName)
            ->field($fields)
            ->where($where)
            ->select();
        return $data;
    }
    /* 随机数条数据
     * $alias:数据表别名
     * $where:条件（带别名条件）
     * */
    public function rand_list($where,$fields,$limit,$order,$type2,$condition2){
        $data=Db::name($this->tableName)->alias('w')->field($fields)
            ->join($type2,$condition2,'left')
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->select();
        return $data;
    }
    /* 购物车列表
     *
     * $where:条件（带别名条件）
     * */
    public function cart_list($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields,$order){
        $data=Db::name($this->tableName)->alias('w')->field($fields)
            ->join($type1,$condition1,'left')
            ->join($type2,$condition2,'left')
            ->join($type3,$condition3,'left')
            ->where($where)
            ->order($order)
            ->select();
        return $data;
    }
    public function cart_sort($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields,$group,$order){
        $data=Db::name($this->tableName)->alias('w')->field($fields)
            ->join($type1,$condition1,'left')
            ->join($type2,$condition2,'left')
            ->join($type3,$condition3,'left')
            ->where($where)
            ->order($order)
            ->group($group)
            ->select();
        return $data;
    }
}